package com.zw.separate.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Druid的DataResource配置类
 * @author Raye
 * @since 2016年10月7日14:14:18
 */
@Configuration
@EnableTransactionManagement
public class DataBaseConfiguration{

//    @Value("${threadConfig.corePoolSize}")
//    private int corePoolSize;
//    @Value("${threadConfig.maxPoolSize}")
//    private int maxPoolSize;
//    @Value("${threadConfig.keepAliveTime}")
//    private int keepAliveTime;
//    @Value("${threadConfig.queueCapacity}")
//    private int queueCapacity;

    public DataBaseConfiguration(){
    	System.out.println("#################### 开始配置双数据源 DataBaseConfiguration ####################");
    }


    public DataSource master() {
        System.out.println("Master druid！！！");
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl("jdbc:mysql://192.168.0.39:3306/demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai");
        datasource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        datasource.setUsername("root");
        datasource.setPassword("root");
        datasource.setInitialSize(1);
        datasource.setMinIdle(3);
        datasource.setMaxWait(600);
        datasource.setMaxActive(20);
        datasource.setMinEvictableIdleTimeMillis(300000);
        try {
			datasource.setFilters("stat,wall");
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return datasource;
    }

    public DataSource slave() {
        System.out.println("Slave druid！！！");
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl("jdbc:mysql://192.168.0.158:3306/demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai");
        datasource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        datasource.setUsername("root");
        datasource.setPassword("xnhdfx123456");
        datasource.setInitialSize(1);
        datasource.setMinIdle(3);
        datasource.setMaxWait(600);
        datasource.setMaxActive(20);
        datasource.setMinEvictableIdleTimeMillis(300000);
        try {
            datasource.setFilters("stat,wall");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return datasource;
    }

    /**
     * @Description 动态数据源 默认为主库
     * @Author zw
     * @Date 2021/5/19 16:12
     * @Param []
     * @Return wang.raye.separate.config.DynamicDataSource
    **/
    @Bean
	public DynamicDataSource dynamicDataSource() {
    	DataSource master = master();
		DataSource slave = slave();
		Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
		targetDataSources.put(DynamicDataSource.DatabaseType.Master, master);
		targetDataSources.put(DynamicDataSource.DatabaseType.Slave, slave);

		DynamicDataSource dataSource = new DynamicDataSource();
		dataSource.setTargetDataSources(targetDataSources);// 该方法是AbstractRoutingDataSource的方法
		dataSource.setDefaultTargetDataSource(master);
		return dataSource;
	}


}
